import express, {Express,Request,Response,NextFunction} from 'express';
import createError from 'http-errors';
import {INTERNAL_SERVER_ERROR, NOT_FOUND} from 'http-status-codes';
import logger from 'morgan';
let app: Express = express();
import indexRouter from './src/routes/index';
import usersRouter from './src/routes/users';

app.use(logger('dev')); // 打印请求日志的
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use(function (req: Request, res: Response, next: NextFunction) {
  next(createError(NOT_FOUND))
});
app.use(function (error: any, req: Request, res: Response, next: NextFunction) {
  res.status(error.status || INTERNAL_SERVER_ERROR);
  res.json({
    success: false,
    error
  })
});

export default app;
